<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>近日天气查询</title>
    <style>
        /* 全局样式 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        /* 容器样式 */
        .container {
            background-color: #ffffff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 300px;
            text-align: center;
        }

        /* 标题样式 */
        h1 {
            font-size: 24px;
            color: #333333;
            margin-bottom: 20px;
        }

        /* 表单样式 */
        #weatherForm {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        #cityId {
            padding: 10px;
            border: 1px solid #cccccc;
            border-radius: 5px;
            font-size: 14px;
        }

        button {
            padding: 10px;
            background-color: #007bff;
            color: #ffffff;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #0056b3;
        }

        /* 天气信息样式 */
        #weatherResult {
            margin-top: 20px;
            text-align: left;
        }

        #weatherResult h2 {
            font-size: 20px;
            color: #007bff;
            margin-bottom: 10px;
        }

        #weatherResult p {
            font-size: 14px;
            color: #555555;
            margin: 5px 0;
        }

        /* 错误信息样式 */
        .error {
            color: #ff0000;
            font-size: 14px;
            margin-top: 10px;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
    <h1>近日天气查询</h1>
    <form id="weatherForm">
        <input type="text" id="cityId" name="cityId" placeholder="请输入城市ID" required>
        <button type="submit">查询天气</button>
    </form>
    <div id="weatherResult"></div>
</div>

<script>
    $(document).ready(function() {
        $('#weatherForm').on('submit', function(event) {
            event.preventDefault(); // 阻止表单默认提交行为

            var cityId = $('#cityId').val();

            $.ajax({
                url: '/weather',
                type: 'GET',
                data: {
                    cityId: cityId
                },
                success: function(response) {
                    if (response.code === 200) {
                        var weatherData = response.result;
                        var html = '<h2>' + weatherData.area + ' 天气</h2>';
                        html += '<p>日期: ' + weatherData.date + '</p>';
                        html += '<p>星期: ' + weatherData.week + '</p>';
                        html += '<p>天气: ' + weatherData.weather + '</p>';
                        html += '<p>提示: ' + weatherData.tips + '</p>';
                        $('#weatherResult').html(html);
                    } else {
                        $('#weatherResult').html('<p class="error">获取天气信息失败: ' + response.msg + '</p>');
                    }
                },
                error: function() {
                    $('#weatherResult').html('<p class="error">请求失败，请稍后重试。</p>');
                }
            });
        });
    });
</script>
</body>
</html>